package com.goods.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.CategoriesMapper;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.service.CategoriesServices;
import com.goods.common.error.BaseError;
import com.goods.common.error.BusinessCodeEnum;
import com.goods.common.error.BusinessException;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductCategoryTreeNode;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author ：MJS
 * @date ：Created in 2021/6/7  14:28
 * @description：类别列表展示
 */
@Service
public class CategoriesServicesImpl implements CategoriesServices {
    @Autowired(required = false)
    private CategoriesMapper categoriesMapper;

    @Autowired(required =false)
    private ProductMapper productMapper;
    //加载分类数据
    @Override
    public PageVO<ProductCategoryTreeNodeVO> findCategories(Integer pageNum, Integer pageSize, ProductCategoryTreeNodeVO productCategoryTreeNodeVO) {
        //开启分页
        PageHelper.startPage(pageNum, pageSize);
        //查询一级分类
        Example o = new Example(ProductCategoryTreeNode.class);
        o.createCriteria().andEqualTo("pid", 0);
        //获取一级分类
        List<ProductCategoryTreeNode> productCategoryTreeNodes = categoriesMapper.selectByExample(o);
        List<ProductCategoryTreeNodeVO> ProductCategoryTreeNodeVOS = new ArrayList<>();
        //判断是否为空
        if (!CollectionUtils.isEmpty(productCategoryTreeNodes)) {
            //使用偷天换日
            productCategoryTreeNodes.stream().map(
                    productCategoryTreeNode -> {
                        //通过一级分类查询二级分类，二级分类的pid是一级分类的id
                        Example o1 = new Example(ProductCategoryTreeNode.class);
                        o1.createCriteria().andEqualTo("pid", productCategoryTreeNode.getId());
                        //获取二级分类
                        List<ProductCategoryTreeNode> productCategoryTreeNodes1 = categoriesMapper.selectByExample(o1);
                        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOs1 = new ArrayList<>();
                        //判断是否为空
                        if (!CollectionUtils.isEmpty(productCategoryTreeNodes1)) {
                            productCategoryTreeNodes1.stream().map(
                                    productCategoryTreeNode1 -> {
                                        //通过二级分类查询三级分类，三级分类的pid是二级分类的id
                                        Example o2 = new Example(ProductCategoryTreeNode.class);
                                        o2.createCriteria().andEqualTo("pid", productCategoryTreeNode1.getId());
                                        //获取三级分类
                                        List<ProductCategoryTreeNode> productCategoryTreeNodes2 = categoriesMapper.selectByExample(o2);
                                        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOs2=new ArrayList<>();
                                        //判断是否为空
                                        if(!CollectionUtils.isEmpty(productCategoryTreeNodes2)){
                                            productCategoryTreeNodes2.stream().map(
                                                    productCategoryTreeNode2->{
                                                        ProductCategoryTreeNodeVO c3 = new ProductCategoryTreeNodeVO();
                                                        //将实体类中的数据传给vo实体类
                                                        BeanUtils.copyProperties(productCategoryTreeNode2,c3);
                                                        return  productCategoryTreeNodeVOs2.add(c3);
                                                    }
                                            ).collect(Collectors.toList());
                                        }
                                        //将三级分类赋值进去
                                        productCategoryTreeNode1.setChildren(productCategoryTreeNodeVOs2);
                                        ProductCategoryTreeNodeVO c2 = new ProductCategoryTreeNodeVO();
                                        //将实体类中的数据传给vo实体类
                                        BeanUtils.copyProperties(productCategoryTreeNode1, c2);
                                        //设置分类等级
                                        c2.setLev(2);
                                        return productCategoryTreeNodeVOs1.add(c2);
                                    }
                            ).collect(Collectors.toList());
                        }
                        //将二级分类赋值
                        productCategoryTreeNode.setChildren(productCategoryTreeNodeVOs1);
                        ProductCategoryTreeNodeVO c1 = new ProductCategoryTreeNodeVO();
                        //将实体类中的数据传给vo实体类
                        BeanUtils.copyProperties(productCategoryTreeNode, c1);
                        //设置分类等级
                        c1.setLev(1);
                        return ProductCategoryTreeNodeVOS.add(c1);
                    }
            ).collect(Collectors.toList());
        }
        PageInfo<ProductCategoryTreeNode> info = new PageInfo(productCategoryTreeNodes);
        return new PageVO(info.getTotal(), ProductCategoryTreeNodeVOS);
    }
    //加载父级分类数据
    @Override
    public List<ProductCategoryTreeNodeVO> findParentCategoryTree() {
        //查询所有一级分类
        //创建查询条件 pid=0
        Example o = new Example(ProductCategoryTreeNode.class);
        o.createCriteria().andEqualTo("pid",0);
        //获取一级分类列表
        List<ProductCategoryTreeNode> productCategoryTreeNodes = categoriesMapper.selectByExample(o);
        //转换对象
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOs = new ArrayList<>();
        //判断查询列表是否为空
        if (!CollectionUtils.isEmpty(productCategoryTreeNodes)) {
            //循环遍历
            for (ProductCategoryTreeNode productCategoryTreeNode : productCategoryTreeNodes) {
                //查询二级分类
                Example o1 = new Example(ProductCategoryTreeNode.class);
                o1.createCriteria().andEqualTo("pid",productCategoryTreeNode.getId());
                //获取二级分类列表
                List<ProductCategoryTreeNode> productCategoryTreeNodes1 = categoriesMapper.selectByExample(o1);
                //用来转换对象
                List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOs1 = new ArrayList<>();
                //判断是否为空
                if(!CollectionUtils.isEmpty(productCategoryTreeNodes1)){
                    for (ProductCategoryTreeNode productCategoryTreeNode1 : productCategoryTreeNodes1) {
                        ProductCategoryTreeNodeVO productCategoryTreeNodeVO = new ProductCategoryTreeNodeVO();
                        //转成Vo
                        BeanUtils.copyProperties(productCategoryTreeNode1,productCategoryTreeNodeVO);
                        //添加到集合中
                        productCategoryTreeNodeVOs1.add(productCategoryTreeNodeVO);
                    }
                }
                //将二级分类添加到一级分类中
                productCategoryTreeNode.setChildren(productCategoryTreeNodeVOs1);
                ProductCategoryTreeNodeVO productCategoryTreeNodeVO = new ProductCategoryTreeNodeVO();
                //工具类赋值
                BeanUtils.copyProperties(productCategoryTreeNode, productCategoryTreeNodeVO);
                productCategoryTreeNodeVOs.add(productCategoryTreeNodeVO);
            }
        }
        //前端返回需要的对象
        return productCategoryTreeNodeVOs;
    }

    //添加分类
    @Override
    public void add(ProductCategoryTreeNodeVO productCategoryTreeNodeVO) {

        ProductCategoryTreeNode productCategoryTreeNode = new ProductCategoryTreeNode();
        //将VO类传给实体类
        BeanUtils.copyProperties(productCategoryTreeNodeVO,productCategoryTreeNode);
        productCategoryTreeNode.setCreateTime(new Date());
        productCategoryTreeNode.setModifiedTime(new Date());
        categoriesMapper.insert(productCategoryTreeNode);
    }
    //数据回显
    @Override
    public ProductCategoryTreeNode edit(Long id) {
        ProductCategoryTreeNode productCategoryTreeNode = new ProductCategoryTreeNode();
        productCategoryTreeNode.setId(id);
        return  categoriesMapper.selectByPrimaryKey(productCategoryTreeNode);
    }
    //修改分类数据
    @Override
    public void update(Long id,ProductCategoryTreeNode productCategoryTreeNode) {
        productCategoryTreeNode.setModifiedTime(new Date());
        categoriesMapper.updateByPrimaryKey(productCategoryTreeNode);
    }
    //删除数据
    @Override
    public boolean delete(Long id) {

        ProductCategoryTreeNode productCategoryTreeNode = new ProductCategoryTreeNode();
        productCategoryTreeNode.setId(id);
        //查询出来我想删除的分类
        ProductCategoryTreeNode productCategoryTreeNode1 = categoriesMapper.selectByPrimaryKey(productCategoryTreeNode);
        //查找我想删除的分类是否是其他类的父类
        Example o = new Example(ProductCategoryTreeNode.class);
        o.createCriteria().andEqualTo("pid",productCategoryTreeNode1.getId());
        List<ProductCategoryTreeNode> productCategoryTreeNodes = categoriesMapper.selectByExample(o);
        //查找我想删除的类是否有物资引用的类
        Example p = new Example(Product.class);
        p.createCriteria().andEqualTo("threeCategoryId",productCategoryTreeNode1.getId());
        List<Product> products = productMapper.selectByExample(p);
        if(productCategoryTreeNodes.size()==0 && products.size()==0){
             categoriesMapper.delete(productCategoryTreeNode);
             return true;
        }
        return false;
    }

}